<template>
  <!-- 执行记录 -->
  <div>
    <span class="formTitle"><i class="vertical-bar"></i>{{ title }}</span>
    <upper-template @search="searchFunc" @clear="searchForm.dataDate = ''" :configIcon="false">
      <div slot="buttonArea">
        <el-button @click="expExcel()">导出</el-button>
        <el-button @click="toSubRecord()">子任务</el-button>
        <el-button @click="goBackIndex()">返回</el-button>
      </div>
      <div slot="searchArea">
        <el-form
          :model="searchForm"
          ref="searchForm"
          label-width="100px"
          label-position="right"
          size="mini"
        >
          <el-form-item label="任务编号：" prop="taskCode">
            <el-input
              v-model="searchForm.taskCode"
              style="width:150px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="任务名称：" prop="taskName">
            <el-input
              v-model="searchForm.taskName"
              style="width:150px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="数据日期：" prop="dataDate">
            <el-date-picker
              v-model="searchForm.dataDate"
              type="date"
              style="width:150px;"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
       </el-form>
      </div>
    </upper-template>

    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      @row-click="rowClick"
    >
      <el-table-column width="55" type="selection"  />

      <el-table-column
        prop="pricingCode"
        label="定价编号"
        show-overflow-tooltip
      />
      <el-table-column prop="dataDate" label="数据日期" show-overflow-tooltip />
      <el-table-column
        prop="sceneCode"
        label="情景编号"
        show-overflow-tooltip
      />
      <el-table-column
        prop="sceneVersion"
        label="情景版本"
        show-overflow-tooltip
      />
      <el-table-column prop="startTm" label="开始时间" show-overflow-tooltip />
      <el-table-column prop="endTm" label="结束时间" show-overflow-tooltip />
      <el-table-column
        prop="successNum"
        label="成功记录"
        show-overflow-tooltip
      />
      <el-table-column prop="failNum" label="失败记录" show-overflow-tooltip />
      <el-table-column prop="status" label="执行状态" show-overflow-tooltip :formatter="formatRunStatus"/>
     </el-table>
        <pagination
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"
          :current-page="searchForm.page"
          :page-size="searchForm.size"
          @refresh="searchFunc"
          :total="totals"
        ></pagination>
       
     
    <!-- 定价子任务页面     --------------------------------------------------------- -->
    <el-dialog
      v-dialogDrag
      :title="titleName"
      :visible.sync="subRecord"
      width="1024px"
      :append-to-body="true"
    >
      <el-form
        :model="recordForm"
        ref="subRecordRef"
        label-width="100px"
        label-position="right"
      >
        <el-row>
          <el-col :span="5">
            <el-form-item label="任务名称：" prop="taskName">
              <el-input
                v-model="recordForm.taskName"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-form-item label="定价编号：" prop="pricingCode">
              <el-input
                v-model="recordForm.pricingCode"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-form-item label="开始日期：" prop="taskSd">
              <el-input
                v-model="recordForm.taskSd"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-form-item label="结束日期：" prop="taskEd">
              <el-input
                v-model="recordForm.taskEd"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="container">
          <el-col style="margin-right:10px;" class="content-left">
            <upper-template :searchIcon="false" :configIcon="false">
              <!-- 操作按钮区 -->
              <template slot="buttonArea">
                <el-button @click="toBlock()">引擎任务</el-button>
                <el-button @click="toRecord()">执行记录</el-button>
                <el-button @click="goBack()">返回</el-button>
              </template>
            </upper-template>
          </el-col>
        </el-row>
        <el-table
          ref="subRecordModelTable"
          :data="subRecordModelData"
          style="width: 100%"
          @row-click="authorizeModelFor"
        >
          <el-table-column
            prop="pricingCode"
            label="定价编号"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="subTaskCode"
            label="子任务编号"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="dataDate"
            label="数据日期"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="startTm"
            label="开始时间"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="endTm"
            label="结束时间"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="status"
            label="运行状态"
            :show-overflow-tooltip="true"
          ></el-table-column>
        </el-table>
        <pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="recordForm.page"
          :page-size="recordForm.size"
          @refresh="toSubRecord"
          :total="total"
        ></pagination>
      </el-form>
    </el-dialog>

    <!-- 定价引擎任务页面     --------------------------------------------------------- -->
    <el-dialog
      v-dialogDrag
      :title="blocktitleName"
      :visible.sync="block"
      width="1024px"
      :append-to-body="true"
    >
      <el-form
        :model="blockForm"
        ref="blockRef"
        label-width="100px"
        label-position="right"
      >
        <el-row>
          <el-col :span="5">
            <el-form-item label="任务名称：" prop="taskName">
              <el-input
                v-model="blockForm.taskName"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-form-item label="定价编号：" prop="pricingCode">
              <el-input
                v-model="blockForm.pricingCode"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-form-item label="数据日期：" prop="dataDate">
              <el-input
                v-model="blockForm.dataDate"
                maxlength="32"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="5" :offset="1">
            <el-form-item label="表名称：" prop="positions">
              <el-select v-model="blockForm.pdName" placeholder="请选择">
                <el-option
                  v-for="item in positionOptions"
                  :key="item.pdPostfix"
                  :label="item.pdName"
                  :value="item.pdPostfix"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="5" :offset="1">
            <el-form-item label="运行状态：" prop="status">
              <el-select
                v-model="blockForm.status"
                style="width:calc(100% - 20px)"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row class="container">
          <el-col style="margin-right:10px;" class="content-left">
            <upper-template :searchIcon="false" :configIcon="false">
              <!-- 操作按钮区 -->
              <template slot="buttonArea">
                <el-button @click="blockquery()">查询</el-button>
                <el-button @click="toRecord()">查看结果</el-button>
                <el-button @click="toRecord()">执行记录</el-button>
                <el-button @click="toSubRecord()">子任务</el-button>
                <el-button @click="goBack()">返回</el-button>
              </template>
            </upper-template>
          </el-col>
        </el-row>
        <el-table
          ref="blockModelTable"
          :data="blockModelData"
          style="width: 100%"
          @row-click="authorizeModelFor"
        >
          <el-table-column
            prop="tableName"
            label="表名称"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="blockSize"
            label="块大小"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="dataDate"
            label="数据日期"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="startAccount"
            label="开始编号"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="endAccount"
            label="结束编号"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="successNum"
            label="成功记录数"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="failNum"
            label="失败记录数"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="startTm"
            label="开始时间"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="endTm"
            label="结束时间"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="status"
            label="运行状态"
            :show-overflow-tooltip="true"
          ></el-table-column>
        </el-table>
        <pagination
          @size-change="blockhandleSizeChange"
          @current-change="blockhandleCurrentChange"
          :current-page="blockForm.page"
          :page-size="blockForm.size"
          @refresh="toBlock"
          :total="blocktotal"
        ></pagination>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from "@/api/getDict";
import { pricingTask } from "@/api/index";
import moment from "moment"
export default {
  props: {
    //选中的值
    baseData: {
      type: Object,
      default: ()=>({})
    },
    title:String
  },
  data() {
    return {
      totals: null,
      blocktotal: null,
      total: null,
      //引擎任务页面
      positionOptions: {},
      statusOption: {},
      blocktitleName: "引擎任务",
      blockModelData: [],
      blockForm: {},
      block: false,
      //定价子任务表单数据
      subRecordModelData: [],
      //定价子任务界面上方数据
      recordForm: { taskName: "", pricingCode: "", dataDate: "" },
      titleName: "定价子任务",
      subRecord: false,
      searchForm: {
        taskCode:this.baseData.taskCode,
        taskName:this.baseData.taskName,
        dataDate:this.baseData.dataDate
      },
      tableData: [],
      textData: [
        { prop: "pricingCode", label: "定价编号", disabled: true },
        { prop: "dataDate", label: "数据日期", disabled: true },
        { prop: "sceneCode", label: "情景编号", disabled: true },
        { prop: "sceneVersion", label: "情景版本", disabled: true },
        { prop: "startTm", label: "开始时间", disabled: true },
        { prop: "endTm", label: "结束时间", disabled: true },
        { prop: "successNum", label: "成功记录", disabled: true },
        { prop: "failNum", label: "失败记录", disabled: true },
        { prop: "status", label: "执行状态", disabled: true }
      ],
      checkList: []
    };
  },
  watch: {},
  components: {},
  created() {
    this.searchFunc();
  },
  computed: {},
  mounted() {},
  methods: {
    //导出执行记录
    expExcel(){
         //判断是根据条件查询  还是根据选择查询
      let data = {
        taskCode: this.searchForm.taskCode,
        taskName: this.searchForm.taskName,
        dataDate: this.searchForm.dataDate

      };
      pricingTask.excuteRecord(data).then(response => {
           console.log(response,"response");
        if (response.code == 500) {
          this.$message.error(response.message);
        } else if (response) {
          console.log(decodeURI(response.headers["content-disposition"]));
          var filename = decodeURI(
            response.headers["content-disposition"]
          ).split("=")[1];
          let contenttype =
            filename.substring(filename.lastIndexOf("."), filename.length) ===
            "xlsx"
              ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
              : "application/vnd.ms-excel;charset=utf-8";

          var blob = new Blob([response.data], {
            type: contenttype
          });
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, filename);
          } else {
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
          this.$message.success("导出成功");
        } else {
          this.$message.error(response.message || "获取失败");
        }
      });


    },
        formatRunStatus(row, column) {
      if (row.status == "INIT") {
        return "初始化";
      } else if (row.status == "PREPARING") {
        return "准备中";
      } else if (row.status == "RUNNING") {
        return "运行中";
      } else if (row.status == "FINISH") {
        return "已完成";
      } else if (row.status == "ERROR") {
        return "错误";
      } else if (row.status == "SENDBACK") {
        return "已退回";
      } else if (row.status == "RETRY") {
        return "再次尝试中";
      } else {
        return "";
      }
    }, 
    blockquery() {
      let data = {
        taskCode: this.blockForm.taskCode,
        pricingCode: this.blockForm.pricingCode,
        dataDate: this.recordForm.dataDate,
        tableName: this.blockForm.pdName,
        status: this.blockForm.status,
        page: this.blockForm.page,
        size: this.blockForm.size
      };
      console.log(data, "点击查询按钮的时候传递的参数");
      pricingTask.findAllBlock(data).then(res => {
        if (res.code == 200) {
          this.blockModelData = res.datas.rows;
        }
        console.log(this.blockModelData, "blockModelData");
      });
    },
    handleSizeChanges(val) {
      this.searchForm.size = val;
      this.searchFunc();
      console.log(val, "每页数");
    },
    handleCurrentChanges(val) {
      this.searchForm.page = val;
      this.searchFunc();
      console.log(val, "当前页");
    },
    handleSizeChange(val) {
      this.recordForm.size = val;
      this.toSubRecord();
      console.log(val, "每页数");
    },
    handleCurrentChange(val) {
      this.recordForm.page = val;
      this.toSubRecord();
      console.log(val, "当前页");
    },
    blockhandleSizeChange(val) {
      this.blockForm.size = val;
      this.toBlock();
      console.log(val, "每页数");
    },
    blockhandleCurrentChange(val) {
      this.blockForm.page = val;
      this.toBlock();
      console.log(val, "当前页");
    },
    goBackIndex(){
      this.$emit("backIndex","")
    },
    goBack() {
      this.subRecord = false;
      this.block = false;
      this.$emit("goBack", "");
    },
    getstatus(data) {
      getDict(data).then(res => {
        if (res.code == 200) {
          this.statusOption = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    toBlock() {
      this.getstatus("ftp_pricing_task_status");
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      let data = { pricingCode: selection[0].pricingCode };
      pricingTask
        .toBlockSceneTask('"' + selection[0].pricingCode + '"')
        .then(res => {
          console.log(res, "跳转子任务的时候chaxundaodeshujv==============");
          if (res.code == 200) {
            this.positionOptions = res.datas.positions;
          }
          console.log(this.recordForm, "recordForm");
        });
      this.blockForm.taskName = this.recordForm.taskName;
      this.blockForm.pricingCode = this.recordForm.pricingCode;
      this.blockForm.dataDate = this.recordForm.dataDate;
      console.log(this.blockForm, "blockForm");
      pricingTask.findAllBlock(data).then(res => {
        console.log(res, "跳转引擎任务的时候chaxundaodeshujv==============");
        if (res.code == 200) {
          this.blockModelData = res.datas.rows;
        }
        console.log(this.blockModelData, "blockModelData");
      });
      this.block = true;
      this.subRecord = false;
    },
    toRecord() {
      this.subRecord = false;
      this.block = false;
    },
    searchFunc() {
      let data = { 
        taskCode: this.searchForm.taskCode,
        taskName: this.searchForm.taskName,
        dataDate: this.searchForm.dataDate,
        size: this.searchForm.size,
        page: this.searchForm.page };
      console.log(data, "执行记录页面加载的时候传递的参数");
      pricingTask.findAllRecord(data).then(res => {
        console.log(res, "执行记录页面加载的时候查出来的数据==============");
        if (res.code == 200) {
          this.tableData = res.datas.rows.map(v=>{
            let obj = v;
            obj.dataDate =obj.dataDate? moment(obj.dataDate).format('yyyy-MM-DD'):"-"
            return obj;
          });
          this.totals = res.datas.total
        }
      });
    },
    toSubRecord() {
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      let data = { pricingCode: selection[0].pricingCode };
      pricingTask
        .toBlockSceneTask('"' + selection[0].pricingCode + '"')
        .then(res => {
          // console.log(res, "跳转子任务的时候查到的上方列表的的数据=============");
          if (res.code == 200) {
            this.recordForm.taskName = res.datas.task.taskName;
            this.recordForm.pricingCode = selection[0].pricingCode;
            this.recordForm.taskSd = res.datas.task.taskSd;
            this.recordForm.taskEd = res.datas.task.taskEd;
            this.$emit('toSubTasks',this.recordForm)
          }
          console.log(this.recordForm, "recordForm");
        });
        
    },
    //点击行选中
    authorizeModelFor(row) {
      if (this.$refs.subRecordModelTable) {
        this.$refs.subRecordModelTable.clearSelection();
      }
      this.$refs.subRecordModelTable.toggleRowSelection(row, true);
    },
    rowClick(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
    }
  }
};
</script>
